/**
************************************************************************************************
* @file   		tcp_demo.c
* @author  		WIZnet Software Team 
* @version 		V1.0
* @date    		2015-02-14
* @brief   		TCP 演示函数
* @attention  
************************************************************************************************
**/
#include "main.h"
#include <stdio.h>
#include <string.h>
#include "tcp_demo.h"
#include "W5500_conf.h"
#include "w5500.h"
#include "socket.h"

#define   DATA_MAX_IN 2048
uint8 buff[DATA_MAX_IN+1];				                              	         /*定义一个2KB的缓存*/
uint8_t uLinkFlag = 0; 
char Buf[] = {"欢迎使用网络开发板: 设备已经连接"};

#define TCP_DATA_LENGTH 12

unsigned char TCP_IN[TCP_DATA_LENGTH];    //TCP通信接收数组
unsigned char TCP_OUT[TCP_DATA_LENGTH];   //TCP通信发送数组

uint16 return_data;




/**
*@brief		TCP Server回环演示函数。
*@param		无
*@return	无
*/
unsigned char key1[]={"有按键被按下"};
unsigned char key2[5]={0XAB,0XBB,0XBB,0XBB,0XAA};
uint16 maxlen;


//void rx_length(uint8 * huan);
void Make_TCP_OUT(void);
void Make_TCP_IN(uint8_t *tcp_in,uint16_t len);

void do_tcp_server(void)
{	
	uint16 len=0; 
	//debug_data
  if(len>maxlen)
	{
	   len=maxlen; 
	}	
  //debug_data	
	switch(getSn_SR(SOCK_TCPS))											            	/*获取socket的状态*/
	{
		case SOCK_CLOSED:													                  /*socket处于关闭状态*/
			socket(SOCK_TCPS ,Sn_MR_TCP,local_port,Sn_MR_ND);	        /*打开socket*/
		   uLinkFlag = 1;break;     
    
		case SOCK_INIT:														                  /*socket已初始化状态*/
			listen(SOCK_TCPS);												                /*socket建立监听*/
     
		  break;
		
		case SOCK_ESTABLISHED:												              /*socket处于连接建立状态*/
		  if(uLinkFlag == 1){send(SOCK_TCPS,(const uint8 *)Buf,sizeof("欢迎使用网络开发板: 设备已经连接")); uLinkFlag = 0;}		
		
			if(getSn_IR(SOCK_TCPS) & Sn_IR_CON)
			{//send(SOCK_TCPS,(uint8 *)Buf,sizeof(Buf)); LED1_TOGGLE;
				setSn_IR(SOCK_TCPS, Sn_IR_CON);								          /*清除接收中断标志位*/
			}
			len=getSn_RX_RSR(SOCK_TCPS);									            /*定义len为已接收数据的长度*/
			if(len>0)
			{
				recv(SOCK_TCPS,buff,len);								              	/*接收来自Client的数据*/
				buff[len]=0x00; 											                          /*添加字符串结束符*/
				//rx_length(buff);
				printf("%s\r\n",buff);
				
				send(SOCK_TCPS,buff,len);									            /*向Client发送数据*/
       
		  }
		  break;
		
		case SOCK_CLOSE_WAIT:												                /*socket处于等待关闭状态*/
			close(SOCK_TCPS);
		  break;
	}
		//return_data=CRC_calculate_crc16(0xffff,0x8005,0,1,1,test_data,8);
}
	


void do_tcp_server_USER(int a)
{	
  uint16 len=0; 
	switch(getSn_SR(SOCK_TCPS))											            	/*获取socket的状态*/
	{
		case SOCK_CLOSED:													                  /*socket处于关闭状态*/
			socket(SOCK_TCPS ,Sn_MR_TCP,local_port,Sn_MR_ND);	        /*打开socket*/
		  uLinkFlag = 1;break;     
    
		case SOCK_INIT:														                  /*socket已初始化状态*/
			listen(SOCK_TCPS);												                /*socket建立监听*/
     
		  break;
		
		case SOCK_ESTABLISHED:												              /*socket处于连接建立状态*/	
			if(len>DATA_MAX_IN){len=DATA_MAX_IN;}
			if(len>0)
			{
         recv(SOCK_TCPS,buff,len);								              	/*接收来自Client的数据*/
		     buff[len]=0x00; 											                  /*添加字符串结束符*/
				 //Make_TCP_IN(buff,len);     				
		  }
			
			//Make_TCP_OUT();
			send(SOCK_TCPS,TCP_OUT,12);                                //发送	
			if(getSn_IR(SOCK_TCPS) & Sn_IR_CON)
			{//send(SOCK_TCPS,(uint8 *)Buf,sizeof(Buf)); LED1_TOGGLE;
				 setSn_IR(SOCK_TCPS, Sn_IR_CON);								          /*清除接收中断标志位*/
			}
		  break;
		
		case SOCK_CLOSE_WAIT:												                /*socket处于等待关闭状态*/
			close(SOCK_TCPS);
		  break;
	}
//	return_data=CRC_calculate_crc16(0xffff,0x8005,0,1,1,test_data,8);
}

/**
*@brief		TCP Client回环演示函数。
*@param		无
*@return	无
*/

void do_tcp_client(void)
{	
   uint16 len=0;	

	switch(getSn_SR(SOCK_TCPC))								  				         /*获取socket的状态*/
	{
		case SOCK_CLOSED:											        		         /*socket处于关闭状态*/
			socket(SOCK_TCPC,Sn_MR_TCP,local_port++,Sn_MR_ND);
		  break;
		
		case SOCK_INIT:													        	         /*socket处于初始化状态*/
			connect(SOCK_TCPC,remote_ip,remote_port);                /*socket连接服务器*/ 
		  break;
		
		case SOCK_ESTABLISHED: 												             /*socket处于连接建立状态*/
			if(getSn_IR(SOCK_TCPC) & Sn_IR_CON)
			{
				setSn_IR(SOCK_TCPC, Sn_IR_CON); 							         /*清除接收中断标志位*/
			}
		
			len=getSn_RX_RSR(SOCK_TCPC); 								  	         /*定义len为已接收数据的长度*/
			if(len>0)
			{
				recv(SOCK_TCPC,buff,len); 							   		         /*接收来自Server的数据*/
				buff[len]=0x00;  											                 /*添加字符串结束符*/
				printf("%s\r\n",buff);
				send(SOCK_TCPC,key1,12);								     	         /*向Server发送数据*/
			}		  
		  break;
			
		case SOCK_CLOSE_WAIT: 											    	         /*socket处于等待关闭状态*/
			close(SOCK_TCPC);
		  break;

	}
}

//使用示例CRC_calculate_crc16(0xffff,0x8005,0,1,1,puchMsg,usDataLen)
//puchMsg 传入校验串首地址
//usDataLen 校验字符长度
//CRC16MODBUS校验代码
uint16 CRC_calculate_crc16(uint16 wCRCin,uint16 wCPoly,uint16 wResultXOR,char input_invert,char ouput_invert,unsigned char *puchMsg, int usDataLen)
{
    uint8 wChar = 0;
    while (usDataLen--)
    {
        wChar = *(puchMsg++);
        if(input_invert) //输入值反转
        {
            uint8 temp_char = wChar;
            wChar=0;
            for(int i=0;i<8;++i)
            {
                if(temp_char&0x01)
                    wChar|=0x01<<(7-i);
                temp_char>>=1;
            }
        }
        wCRCin ^= (wChar << 8);
        for (int i = 0; i < 8; i++)
        {
            if (wCRCin & 0x8000)
                wCRCin = (wCRCin << 1) ^ wCPoly;
            else
                wCRCin = wCRCin << 1;
        }
    }
    if(ouput_invert)
    {
        uint16 temp_short = wCRCin;
        wCRCin=0;
        for(int i=0;i<16;++i)
        {
            if(temp_short&0x01)
            wCRCin|=0x01<<(15-i);
            temp_short>>=1;
        }
    }
    return (wCRCin^wResultXOR);
}


